<?php
/***
*    This file is part of Marketorama.
*
*    Marketorama is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Marketorama is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR *PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with Marketorama.  If not, see <http://www.gnu.org/licenses/>.
***/
	/** 
	 * AppsModel is the model related to the Apps entity
	 **/ 
    class AppsModel extends Model {
		/** 
		 * Plain useless constructor
		 **/
        function AppsModel ()
        {
            //call the parent constructor
            parent::Model();
        }
        /**
         * getUserApps returns the list of app of a user
         * @param $userId The Id of the user for which we want the list of apps
         */
        function getUserApps($userId)
        {
            $query="select apps.Id AS AppId, apps.Name as AppName, apps.Description as AppDescription, 
					apps.Public as AppPublic, apps.Url as AppUrl from apps where OwnerId = ? ";    
            $apps=$this->db->query($query,array($userId));
            return $apps->result();
        }
        /**
         * getMyApps returns the list of apps of the current user
         **/
        function getMyApps()
        {
			return $this->getUserApps($this->session->userdata('UserId'));
		}
		/**
		 * valueExists looks for a value in the table apps
		 * @param $field The field to search within
		 * @param $value The value to search
		 * @returns TRUE if $value is found in column $field. FALSE otherwise.
		 **/
        function valueExists($field, $value)
        {
            $this->db->like($field,$value);
            $this->db->from('apps');
            return ($this->db->count_all_results()==0);
        }
        /**
         * insertApp creates a new app. This functions makes a simple insert in the Apps table.
         * @param $name Name of the App. Should be unique.
         * @param $url Url of the App to be used inside the app, Should be unique.
         * @param $package Java package for the app
         * @param $description Description of the app
         * @param $icon path to the icon in the website. TODO: PENDING FOR REVIEW
         * @param $userId Id of the owner
         * @param $public Will the app be visible for everybody?
         **/
        function insertApp($name, $url, $package, $description, $icon, $userId, $public )
        {
            return $this->db->insert('apps', array('Name'=>$name, 
                                                   'Url'=>$url, 
                                                   'Package'=>$package, 
                                                   'Description'=>$description, 
                                                   'Icon'=>$icon,
                                                   'OwnerId'=>$userId,
                                                   'Public'=>$public));
        }
        /**
         * getApp returns the data of the app pointed by the url
         * @param $url The Url of the desired app
         **/
        function getApp($url)
        {
			$this->db->where('Url', $url);
			$record=$this->db->get("apps");
			if ($record->num_rows()!=1) return null;
			else return $record->row(0);
		}
        
    }
?>
